package com.java.grammer.arrays;

import java.util.Arrays;

/**
 * @author luzhikun
 * @date 2020/11/29- 22:14
 */
/*
 * 现在定义如下的一个数组：int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5},
 * 要求将不为0的值存入一个新的数组，生成的新数组为：
 * int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
 */

public class OldArrays_1 {
    public static void main(String[] args) {
        int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int count = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                count++;
            }
        }
        int[] newArr = new int[count];
        int num = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                newArr[num] = oldArr[i];
                num++;
            }
        }
        System.out.print("新数组为：");
        System.out.print(Arrays.toString(newArr));
    }
}

